
const router = require('koa-router')();
const menus = require("../json/menus"); // 菜单
const { writeJson } = require("../sql/sql-json") // 写去json

// 查
router.get('/menus/pageMenus', (ctx) => {
  const { currpage = 1, paseSize = 20 } = ctx.query
  ctx.body = {
    status: "C0000",
    message: "成功",
    result: {
      currpage,
      paseSize,
      data: menus
    }
  }
})

// 增
router.post('/menus/insertMenu', (ctx) => {
  const { pId, id, title, url, icon, sort, status } = ctx.request.body
  let obj = {
    id,
    pId,
    title,
    icon,
    url,
    sort,
    status,
    updateTime: null,
    children: []
  }
  menus.find((item) => {
    if (!pId && id === item.id) {
      menus.push(menus)
      writeJson('menus', menus, () => {
        ctx.body = {
          status: "C0000",
          message: "插入成功",
          result: true
        }
      })
    } else if (pId === item.pid) {
      item.children.push(obj)
      writeJson('menus', menus, () => {
        ctx.body = {
          status: "C0000",
          message: "插入成功",
          result: true
        }
      })
    } else {
      ctx.body = {
        status: "E0002",
        message: "暂无数据",
        result: true
      }
    }
  })
})

// 编辑
router.post('/menus/editMenu', (ctx) => {
  const { pId, id, title, url, icon, sort, status } = ctx.request.body
  menus.find((item) => {
    if (!pId && id === item.id) {
      item.title = title
      item.url = url
      item.icon = icon
      item.sort = sort
      item.status = status
      writeJson('menus', menus, () => {
        ctx.body = {
          status: "C0000",
          message: "编辑成功",
          result: true
        }
      })
    } else if (pId === item.pid) {
      item.children.find((childItem) => {
        if (childItem.id === id) {
          childItem.title = title
          childItem.url = url
          childItem.icon = icon
          childItem.sort = sort
          childItem.status = status
          writeJson('menus', menus, () => {
            ctx.body = {
              status: "C0000",
              message: "编辑成功",
              result: true
            }
          })
        }
      })
    } else {
      ctx.body = {
        status: "E0002",
        message: "暂无数据",
        result: true
      }
    }
  })
})

// 禁用
router.post('/menus/enableMenu', (ctx) => {
  const { pId, id, status } = ctx.request.body
  menus.find((item) => {
    if (!pId && id === item.id) {
      item.status = status
      writeJson('menus', menus, () => {
        ctx.body = {
          status: "C0000",
          message: "禁用成功",
          result: true
        }
      })
    } else if (pId === item.pid) {
      item.children.find((childItem) => {
        childItem.status = status
      })
      writeJson('menus', menus, () => {
        ctx.body = {
          status: "C0000",
          message: "禁用成功",
          result: true
        }
      })
    } else {
      ctx.body = {
        status: "E0002",
        message: "暂无数据",
        result: true
      }
    }
  })
})

// 删除
router.post('/menus/enableMenu', (ctx) => {
  const { pId, id } = ctx.request.body
  menus.find((item) => {
    if (!pId && id === item.id) {
      const index = menus.find((item) => item.id === id)
      menus.splice(index, 1)
      writeJson('menus', menus, () => {
        ctx.body = {
          status: "C0000",
          message: "删除成功",
          result: true
        }
      })
    } else if (pId === item.pid) {
      const index = menus.find((item) => item.id === id)
      menus.splice(index, 1)
      writeJson('menus', menus, () => {
        ctx.body = {
          status: "C0000",
          message: "删除成功",
          result: true
        }
      })
    } else {
      ctx.body = {
        status: "E0002",
        message: "暂无数据",
        result: true
      }
    }
  })
})

module.exports = router

